package week4.day26;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 *
 * 客户端键盘录入一个字符串给服务端，服务端将这个字符串消息输出本地的一个文本文件中
 * 客户端传输一个文本文件给服务端，服务端将文本文件中的数据(以字符串形式)输出到控制台
 * 客户端传输一个文本文件给服务端，服务器接收这个文本文件然后将文本文件复制到本地另一个路径
 * @Author fengzihao
 * @Date 2024-05-10 19:32
 */
public class TCPPractice {

}
class Client{
    public static void main(String[] args) throws IOException {
        // 键盘传入字符串
        new Thread(()->{
            Socket socket = null;
            try {
                socket = new Socket("localhost",8890);
                OutputStream outputStream = socket.getOutputStream();
                Scanner scanner = new Scanner(System.in);
                System.out.println("请输入要传给服务端的字符串消息：");
                String inputMessage = scanner.nextLine();
                outputStream.write(inputMessage.getBytes());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }finally {
                if (socket!=null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();
        // 文件读入，键盘输出
        new Thread(()->{
            Socket socket = null;
            try {
                socket = new Socket("localhost",8891);
                OutputStream outputStream = socket.getOutputStream();
                InputStream inputStream = new FileInputStream("a.txt");
                int readCount = -1;
                byte[] bytes = new byte[8192];
                while ((readCount=inputStream.read(bytes))!=-1)
                    outputStream.write(bytes,0,readCount);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }finally {
                if (socket!=null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();
        // 文件读入，文件输出
        new Thread(()->{
            Socket socket = null;
            try {
                socket = new Socket("localhost",8892);
                OutputStream outputStream = socket.getOutputStream();
                InputStream inputStream = new FileInputStream("a.txt");
                int readCount = -1;
                byte[] bytes = new byte[8192];
                while ((readCount=inputStream.read(bytes))!=-1)
                    outputStream.write(bytes,0,readCount);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }finally {
                if (socket!=null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();
    }
}
class Server{
    public static void main(String[] args) throws IOException {
        // 键盘录入，文件存储
        new Thread(()->{
            ServerSocket serverSocket = null;
            OutputStream outputStream = null;
            try {
                serverSocket = new ServerSocket(8890);
                Socket accept = serverSocket.accept();
                InputStream inputStream = accept.getInputStream();
                outputStream = new FileOutputStream("b.txt",true);
                byte[] bytes = new byte[8192];
                int readCount = -1;
                while ((readCount = inputStream.read(bytes))!=-1)
                    outputStream.write(bytes,0,readCount);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }finally {
                try {
                    outputStream.close();
                    serverSocket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }).start();

        // 文件传入，控制台输出
        new Thread(()->{
            ServerSocket serverSocket = null;
            try {
                serverSocket = new ServerSocket(8891);
                Socket accept = serverSocket.accept();
                InputStream inputStream = accept.getInputStream();
                byte[] bytes = new byte[8192];
                int readCount = -1;
                while ((readCount = inputStream.read(bytes))!=-1)
                    System.out.print(new String(bytes,0,readCount));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }finally {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }).start();
        // 文件存入，文件存储
        new Thread(()->{
            ServerSocket serverSocket = null;
            OutputStream outputStream = null;
            try {
                serverSocket = new ServerSocket(8892);
                Socket accept = serverSocket.accept();
                InputStream inputStream = accept.getInputStream();
                outputStream = new FileOutputStream("b.txt",true);
                byte[] bytes = new byte[8192];
                int readCount = -1;
                while ((readCount = inputStream.read(bytes))!=-1)
                    outputStream.write(bytes,0,readCount);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }finally {
                try {
                    outputStream.close();
                    serverSocket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }).start();
    }
}
